//
//  LoginViewController.m
//  TQY
//
//  Created by 甘世清 on 2024/7/6.
//

#import "LoginViewController.h"
#import "KFViewController.h"
@interface LoginViewController ()
@property (nonatomic , strong) UITextField * phonetextField;
@property (nonatomic , strong) UITextField * numberTextField;
@property (nonatomic , strong) UIButton * numberBtn;
@property (nonatomic , strong) UIButton * loginBtn;
@property (nonatomic , assign) BOOL isXieyi;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.zx_navTitle = SQNSLocalString(@"登录");
    [self SQ_Header];
}

- (void)SQ_UIViewForSelfView{
    
    UIImageView * bgImageView = [[UIImageView alloc]init];
    bgImageView.image = SQGetImage(@"二级背景");
    [self.view addSubview:bgImageView];
    [bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.view);
    }];
    
    UIImageView * topImageView = [[UIImageView alloc]init];
    topImageView.image = SQGetImage(@"login插画");
    [self.view addSubview:topImageView];
    [topImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view);
        make.size.mas_equalTo(120);
        make.top.equalTo(self.zx_navBar.mas_bottom).offset(20);
    }];
    
    UILabel * phoneLabel = [[UILabel alloc]init];
    phoneLabel.text = SQNSLocalString(@"手机号");
    phoneLabel.font = FONT_MEDIUM(14);
    phoneLabel.textColor = SQColor333333;
    [self.view addSubview:phoneLabel];
    [phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view).offset(38);
            make.top.equalTo(topImageView.mas_bottom).offset(40);
    }];

    [self.view addSubview:self.phonetextField];
    [self.phonetextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.width.mas_equalTo(SCREEN_WIDETH-56);
        make.height.mas_equalTo(64);
        make.top.equalTo(phoneLabel.mas_bottom).offset(10);
    }];

    UILabel * numberLabel = [[UILabel alloc]init];
    numberLabel.text = SQNSLocalString(@"验证码");
    numberLabel.font = FONT_MEDIUM(14);
    numberLabel.textColor = SQColor333333;
    [self.view addSubview:numberLabel];
    [numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(38);
        make.top.equalTo(phoneLabel.mas_bottom).offset(120);
    }];

    [self.view addSubview:self.numberTextField];
    [self.numberTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.width.mas_equalTo(SCREEN_WIDETH-56);
        make.height.mas_equalTo(64);
        make.top.equalTo(numberLabel.mas_bottom).offset(10);
    }];
 
    [self.numberTextField addSubview: self.numberBtn];
    [self.numberBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.numberTextField.mas_right);
        make.centerY.equalTo(self.numberTextField);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(32);
    }];
    
    UIView * lineView = [[UIView alloc]init];
    lineView.backgroundColor = SQColor(@"#8860FF");
    [self.numberTextField addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(1);
        make.centerY.equalTo(self.numberTextField);
        make.right.equalTo(self.numberBtn.mas_left);
        make.height.mas_equalTo(20);
    }];
 
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtn setTitleColor:SQColor(@"#FFB060") forState:normal];
    [leftBtn addTarget:self action:@selector(SQ_NoCode) forControlEvents:UIControlEventTouchUpInside];
    [leftBtn setTitle:SQNSLocalString(@"收不到验证码?") forState:normal];
    leftBtn.titleLabel.font = FONT_MEDIUM(12);
    [self.view addSubview:leftBtn];
    [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.numberTextField.mas_right);
        make.top.equalTo(self.numberTextField.mas_bottom).offset(12);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(30);
    }];
    
    [self.view addSubview:self.loginBtn];
    [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.height.mas_equalTo(48);
        make.width.mas_equalTo(SCREEN_WIDETH-76);
        make.top.equalTo(self.numberTextField.mas_bottom).offset(106);
    }];

    WS(weakSelf);
    YYLabel * label = [[YYLabel alloc]initWithFrame:CGRectMake(SCREEN_WIDETH/2.f-110, SCREEN_HEIGHT- FootHeight-40, 260, 40)];
    label.textVerticalAlignment = YYTextVerticalAlignmentCenter;
    label.textAlignment = NSTextAlignmentCenter;
    label.numberOfLines = 0;
    [self.view addSubview:label];
    NSDictionary *attributes = @{NSFontAttributeName:FONT_MEDIUM(12) , NSForegroundColorAttributeName: SQColor(@"#333333")};
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:SQNSLocalString(@"我已阅读并同意 《用户协议》《隐私协议》") attributes:attributes];
   
    //设置高亮色和点击事件
    [text yy_setTextHighlightRange:[[text string] rangeOfString:SQNSLocalString(@"《用户协议》")] color:SQColor(@"#8760FF") backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        CommonWebVcView *vc= [[CommonWebVcView alloc]init];
        vc.urlStr =  @"https://taoqingyan.qingtao.fun/agreement.html?id=1";
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
    NSRange range1=[[text string]rangeOfString:SQNSLocalString(@"《用户协议》")];
    YYTextDecoration* deco=[YYTextDecoration decorationWithStyle:(YYTextLineStyleSingle) width:[NSNumber numberWithInt:0] color:[UIColor whiteColor]];
    [text yy_setTextUnderline:deco range:range1];
    
    //设置高亮色和点击事件
    [text yy_setTextHighlightRange:[[text string] rangeOfString:SQNSLocalString(@"《隐私协议》")] color:SQColor(@"#8760FF") backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        CommonWebVcView *vc= [[CommonWebVcView alloc]init];
        vc.urlStr =  @"https://taoqingyan.qingtao.fun/agreement.html?id=2";;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
    NSRange range2=[[text string]rangeOfString:SQNSLocalString(@"《隐私协议》") ];
    YYTextDecoration* deco2=[YYTextDecoration decorationWithStyle:(YYTextLineStyleSingle) width:[NSNumber numberWithInt:0] color:[UIColor whiteColor]];
    [text yy_setTextUnderline:deco2 range:range2];
    label.attributedText = text;
    
    SQCustomButton * leftimage = [[SQCustomButton alloc]initType:SQCustomButtonLeftImageType imageSize:CGSizeMake(15, 15) midmargin:6];
    leftimage.image = SQGetImage(@"选择");
    [self.view addSubview:leftimage];
    [leftimage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(label);
        make.size.mas_equalTo(45);
        make.right.equalTo(label.mas_left);
    }];
    
    [leftimage setTouchBlock:^(SQCustomButton * _Nonnull button) {
        weakSelf.isXieyi = !weakSelf.isXieyi;
        if (weakSelf.isXieyi) {
            button.image = SQGetImage(@"未选择");
        } else {
            button.image = SQGetImage(@"选择");
        }
    }];
}

 

-(void)SQ_numberButtonClick{
    if (![self.phonetextField.text isNotEmpty]) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"手机号不能为空!")];
        return;
    }
    if (self.phonetextField.text.length != 11) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"请输入正确手机号码!")];
        return;
    }
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:self.phonetextField.text forKey:@"phone"];
    [HttpTool post:@"user/verify" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        [self SQ_isCode];
        [self.numberTextField becomeFirstResponder];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
    
}
-(void)SQ_isCode{
    [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"已发送验证码")];
    self.numberBtn.enabled = NO;
    WS(weakSelf);
    [LSTTimer addTimerForTime:60 identifier:@"" handle:^(NSString * _Nonnull day, NSString * _Nonnull hour, NSString * _Nonnull minute, NSString * _Nonnull second, NSString * _Nonnull ms) {
        [weakSelf.numberBtn setTitle:[NSString stringWithFormat:@"%@s",second] forState:normal];
    } finish:^(NSString * _Nonnull identifier) {
        [weakSelf.numberBtn setTitle:SQNSLocalString(@"重新获取") forState:normal];
        weakSelf.numberBtn.enabled = YES;
    } pause:^(NSString * _Nonnull identifier) {
        
    }];
    [self SQ_Header];
}
- (void)SQ_Header{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:@"ticket_switch" forKey:@"key"];
    [HttpTool post:@"config/details" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        NSDictionary * data = [[NSDictionary alloc]initWithDictionary:httpModel.data];
        NSString * value = [NSString stringWithFormat:@"%@",[data objectForKey:@"value"]];
        [Manager SQ_SetObject:value FormKey:@"ticket_switch"];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        
    }];
    [self SQ_footer];
}

-(void)SQ_loginBtnClick{
    if (self.isXieyi) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"请查看并勾选协议")];
        return;
    }
    if (![self.phonetextField.text isNotEmpty]) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"请输入手机号")];
        return;
    }
    if (self.phonetextField.text.length != 11) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"请输入正确手机号")];
        return;
    }
    if (![self.numberTextField.text isNotEmpty]) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"请输入验证码")];
        return;
    }
    if (self.numberTextField.text.length != 6) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"请输入正确验证码")];
        return;
    }
    
    WS(weakSelf);
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:self.phonetextField.text forKey:@"phone"];
    [dic setObject:self.numberTextField.text forKey:@"code"];
    NSString * deviceToken = [Manager SQ_stringForKey:@"hexToken"];
    if ([deviceToken isNotEmpty]) {
        [dic setObject:deviceToken forKey:@"deviceToken"];
    }
    [HttpTool post:@"user/login" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        UserModel * model = [UserModel mj_objectWithKeyValues:httpModel.data];
        [UserModel updateLocalUserModel:model];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[Manager SQ_getViewController]];
        weakSelf.view.window.rootViewController = nav;
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
    
}
-(void)SQ_NoCode{
    KFViewController *  vc  = [[KFViewController alloc]init];
    [self SQ_pushViewControllerFull:vc];
}


-(void)SQ_footer{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:@"app_recommended" forKey:@"key"];
    [HttpTool post:@"config/details" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        NSString * value = [NSString stringWithFormat:@"%@",[httpModel.data objectForKey:@"value"]];
        [Manager SQ_SetObject:value FormKey:@"app_recommended"];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
    }];
}

- (UITextField *)phonetextField{
    if (!_phonetextField) {
        _phonetextField = [[UITextField alloc]init];
        _phonetextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:SQNSLocalString(@"请输入手机号") attributes:@{NSForegroundColorAttributeName : SQColor(@"#D7C8FF")}];
        UIView * bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 36)];
        UIImageView * lefe = [[UIImageView alloc]initWithFrame:CGRectMake(20, 10, 16, 16)];
        lefe.image = SQGetImage(@"手机");
        lefe.contentMode = UIViewContentModeScaleAspectFit;
        [bgView addSubview:lefe];
        _phonetextField.leftViewMode = UITextFieldViewModeAlways;
        _phonetextField.leftView = bgView;
        _phonetextField.font = FONT_SEMIBOLD(14);
        _phonetextField.textColor = SQColor333333;
        _phonetextField.backgroundColor = UIColor.whiteColor;
        _phonetextField.layer.cornerRadius = 32;
        _phonetextField.layer.masksToBounds = YES;
        _phonetextField.keyboardType = UIKeyboardTypeNumberPad;
    }
    return  _phonetextField;
}
- (UITextField *)numberTextField{
    if (!_numberTextField) {
        _numberTextField = [[UITextField alloc]init];
        _numberTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:SQNSLocalString(@"请输入验证码") attributes:@{NSForegroundColorAttributeName : SQColor(@"#D7C8FF")}];
        UIView * bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 36)];
        UIImageView * lefe = [[UIImageView alloc]initWithFrame:CGRectMake(20, 10, 16, 16)];
        lefe.image = SQGetImage(@"验证码");
        lefe.contentMode = UIViewContentModeScaleAspectFit;
        [bgView addSubview:lefe];
        _numberTextField.leftViewMode = UITextFieldViewModeAlways;
        _numberTextField.leftView = bgView;
        _numberTextField.font = FONT_SEMIBOLD(14);
        _numberTextField.textColor = SQColor333333;
        _numberTextField.backgroundColor = UIColor.whiteColor;
        _numberTextField.layer.cornerRadius = 32;
        _numberTextField.layer.masksToBounds = YES;
        _numberTextField.keyboardType = UIKeyboardTypeNumberPad;
    }
    return  _numberTextField;
}

- (UIButton *)numberBtn{
    if (!_numberBtn) {
        _numberBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_numberBtn setTitleColor:SQColor(@"#8860FF") forState:normal];
        [_numberBtn setTitle:SQNSLocalString(@"获取验证码") forState:normal];
        _numberBtn.titleLabel.font = FONT_MEDIUM(12);
        [_numberBtn addTarget:self action:@selector(SQ_numberButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _numberBtn;
}
- (UIButton *)loginBtn{
    if (!_loginBtn) {
        _loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_loginBtn setTitleColor:UIColor.whiteColor forState:normal];
        [_loginBtn setTitle:SQNSLocalString(@"登录") forState:normal];
        _loginBtn.layer.cornerRadius = 24;
        _loginBtn.layer.masksToBounds = YES;
        _loginBtn.titleLabel.font = FONT_MEDIUM(16);
        [_loginBtn addTarget:self action:@selector(SQ_loginBtnClick) forControlEvents:UIControlEventTouchUpInside];
        _loginBtn.backgroundColor = SQColor(@"#000000");
    }
    return _loginBtn;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

@end
